package com.liqing.movecomment.util;

import org.springframework.data.redis.core.RedisTemplate;
import org.springframework.data.redis.core.StringRedisTemplate;
import org.springframework.stereotype.Component;

import javax.annotation.PostConstruct;
import javax.annotation.Resource;
import java.util.Set;
import java.util.concurrent.TimeUnit;


@Component
public class RedisUtils {

    private static RedisUtils INSTANCE = new RedisUtils();
    private static final String PREFIX = "move:";

    @Resource
    private StringRedisTemplate stringRedisTemplate;

    @Resource
    private RedisTemplate redisTemplate;



    public static Boolean zAdd(String key,String value,double score){
          return INSTANCE.redisTemplate.opsForZSet().add(key, value, score);
    }

    public static double zScore(String key,String value){
        return INSTANCE.redisTemplate.opsForZSet().score(key,value);
    }

//   public static void zRange(String key){
//        INSTANCE.redisTemplate.opsForZSet().reveran
//   }





    public static Long increase(String key){
        return INSTANCE.redisTemplate.opsForValue().increment(key);
    }

    public static Long decrease(String key){
        return INSTANCE.redisTemplate.opsForValue().decrement(key);
    }

    public static Long getCount(String key){
        return   INSTANCE.redisTemplate.opsForValue().increment(key,0);
    }

    public static Long setCount(String key,Long count){
        return INSTANCE.redisTemplate.opsForValue().increment(key,count);
    }

    public static Boolean hasKey(String key){
        return INSTANCE.redisTemplate.hasKey(key);
    }

    //获取set的 size
    public static Long getSetCount(String key){
       return INSTANCE.redisTemplate.opsForSet().size(key);
    }

    //拿到set集合
    public static Set<String> getSet(String key){
        return INSTANCE.redisTemplate.opsForSet().members(key);
    }
    //判断集合是否有这个值
    public static Boolean isSetHas(String key, String value){
      return   INSTANCE.redisTemplate.opsForSet().isMember(key,value);
    }

    //取出 set中的值 取消点赞
    public static Long removeSetKey(String key,String value) {
        return INSTANCE.redisTemplate.opsForSet().remove(key,value);
    }

    //set 中添加
    public static Long addSetKey(String key,String value){
        return INSTANCE.redisTemplate.opsForSet().add(key,value);
    }






    public static void deleteKey(String key) {
        INSTANCE.redisTemplate.delete(key);
    }

    // Spring Bean的生命周期
    @PostConstruct
    public void init() {
        INSTANCE.redisTemplate = redisTemplate;
        INSTANCE.stringRedisTemplate = stringRedisTemplate;
    }

    // 可以提供过期时间和时间单位的方法
    public static void setString(String key, String value, long time, TimeUnit timeUnit) {
        INSTANCE.stringRedisTemplate.opsForValue().set(getKey(key), value, time, timeUnit);
    }

    // 可以提供过期时间
    public static void setString(String key, String value, long time) {
        setString(key, value, time, TimeUnit.SECONDS);
    }

    // 不设置过期时间的方法
    public static void setString(String key, String value) {
        INSTANCE.stringRedisTemplate.opsForValue().set(getKey(key), value);
    }

    // 根据key获取string类型数据的方法
    public static String getString(String key) {
        return INSTANCE.stringRedisTemplate.opsForValue().get(getKey(key));
    }

    private static String getKey(String key) {
        return PREFIX + key;
    }

    public static void setObject(String key, Object value, long time, TimeUnit timeUnit) {
        INSTANCE.redisTemplate.opsForValue().set(getKey(key), value, time, timeUnit);
    }

    public static void setObject(String key, Object value, long time) {
        setObject(key, value, time, TimeUnit.SECONDS);
    }

    public static void setObject(String key, Object value) {
        INSTANCE.redisTemplate.opsForValue().set(getKey(key), value);
    }

    public static Object getObject(String key) {
        return INSTANCE.redisTemplate.opsForValue().get(getKey(key));
    }

    // 获取过期时间的方法
    public static long ttl(String key) {
        return INSTANCE.stringRedisTemplate.getExpire(getKey(key));
    }
}
